<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    use HasFactory;

    protected $appends = [
        'pay_status_text'
    ];

    protected $fillable = ['no','title','user_id','product_id','product_sku_id','amount','coupon','address','integral','total_amount','pay_amount','pay_status','remark','paid_at','payment_method','payment_no','refund_no','closed','type','extra'];
    const POSTONLY = ['no','title','user_id','product_id','product_sku_id','amount','coupon','address','integral','total_amount','pay_amount','pay_status','remark','paid_at','payment_method','payment_no','refund_no','closed','type','extra'];

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function getpayStatusTextAttribute()
    {
        if($this->pay_status == 'afoot'){
            return '未支付';
        }
        if($this->pay_status == 'success'){
            return '支付成功';
        }
        if($this->pay_status == 'fail'){
            return '支付失败';
        }
    }

    public function course()
    {
        return $this->hasOne(Course::class,'id','product_id')->select('id','title','cover','type_id','price','original_price','sort','clicks','status','shop_type','tag');
    }

    public function courses()
    {
        return $this->hasMany(Course::class,'id','product_id');
    }

}
